<template>
  <div
    :ref="refName"
    :optionData="optionData"
    :title="title"
    :top="top"
    :right="right"
    :interval="interval"
    style="width: 100%; height: 100%"
  ></div>
</template>

<script>
import * as echarts from "echarts";
export default {
  props: {
    refName: {
      type: String,
      default: "ref",
    },
    optionData: {
      type: Object,
    },
    title: {
      type: String,
      default: "",
    },
    top: {
      type: String,
      default: "0",
    },
    right: {
      type: String,
      default: "0",
    },
    interval: {
      type: Number,
      default: 40,
    },
  },
  data() {
    return {
      chartData: null,
      myChart: null,
    };
  },
  watch: {
    optionData: {
      handler(newVal, oldVal) {
        this.chartData = newVal;
        this.init();
      },
      deep: true,
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      if (this.myChart) {
        this.myChart.clear();
        this.myChart.dispose();
        this.myChart = null;
      }
      this.myChart = echarts.init(this.$refs[this.refName]);
      const length = this.chartData && this.chartData.brokenVo.value.length;
      let left;
      switch (length) {
        case 1:
          left = "27.5%";
          break;
        case 2:
          left = "26%";
          break;
        case 3:
          left = "24.5%";
          break;
        case 4:
          left = "23%";
          break;
        case 5:
          left = "22%";
          break;
        case 6:
          left = "20%";
          break;
        case 7:
          left = "19%";
          break;
      }
      let option = {
        title: [
          {
            show: true,
            text: this.chartData && this.chartData.brokenVo.value,
            left: left,
            top: "38%",
            textStyle: {
              fontWeight: 700,
              fontSize: 36,
              color: "#000",
            },
          },
          {
            show: true,
            text: this.chartData && this.chartData.brokenVo.name,
            left: "21.5%",
            top: "52%",
            textStyle: {
              fontWeight: 400,
              fontSize: 18,
              color: "#555",
            },
          },
        ],
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          top: "10%",
          right: "20%",
          itemGap: 26,
        },
        series: [
          {
            type: "pie",
            radius: ["55%", "75%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderWidth: 2,
              borderColor: "#fff",
            },
            label: {
              show: false,
              position: "center",
              color: "inherit",
            },
            center: ["30%", "50%"],
            labelLine: {
              show: false,
            },
            data: this.chartData && this.chartData.brokenVoList,
          },
        ],
      };
      option && this.myChart.setOption(option);
    },
  },
};
</script>
